package net.skillkill.base.systems;

import net.joke.base.components.Position;
import net.joke.base.components.Renderable;
import net.skillkill.base.SkillKillEntityFactory;

import com.artemis.ComponentMapper;
import com.artemis.Entity;
import com.artemis.EntitySystem;
import com.artemis.utils.ImmutableBag;
import com.artemis.utils.Utils;

public class CollisionSystem extends EntitySystem {

	public CollisionSystem() {
		super(Position.class);
	}

	ComponentMapper<Renderable> renderableMapper;	
	ComponentMapper<Position> positionMapper;	
	
	@Override
	public void initialize() {
		renderableMapper = new ComponentMapper<Renderable>(Renderable.class, world);
		positionMapper = new ComponentMapper<Position>(Position.class, world);
//		velocityMapper = new ComponentMapper<Velocity>(Velocity.class, world);
//		healthMapper = new ComponentMapper<Health>(Health.class, world);
	}
	
	
	@Override
	protected boolean checkProcessing() {
		// TODO Auto-generated method stub
		return true;
	}

	@Override
	protected void processEntities(ImmutableBag<Entity> arg0) {
		// TODO Auto-generated method stub
		ImmutableBag<Entity> faction1 = world.getGroupManager().getEntities(SkillKillEntityFactory.FACTION_1);
		ImmutableBag<Entity> bullet1 = world.getGroupManager().getEntities(SkillKillEntityFactory.BULLET_FACTION_1);
		ImmutableBag<Entity> faction2 = world.getGroupManager().getEntities(SkillKillEntityFactory.FACTION_2);
		ImmutableBag<Entity> bullet2 = world.getGroupManager().getEntities(SkillKillEntityFactory.BULLET_FACTION_2);
		
		// Collisioni possibili :
		// Faction 1 con bullet 2 (applica effetti)
		if(faction1 != null && bullet2 != null) {
			for(int a = 0; faction1.size() > a; a++) {
				Entity ship = faction1.get(a);
				for(int b = 0; bullet2.size() > b; b++) {
					Entity bullet = bullet2.get(b);
					
					if(collisionExists(bullet, ship)) {
						// TODO Collisione
						break;
					}
				}
			}
		}
		// Faction 2 con bullet 1 (applica effetti)
		if(faction2 != null && bullet1 != null) {
			for(int a = 0; faction2.size() > a; a++) {
				Entity ship = faction2.get(a);
				for(int b = 0; bullet1.size() > b; b++) {
					Entity bullet = bullet1.get(b);
					
					if(collisionExists(bullet, ship)) {
						// TODO Collisione
						break;
					}
				}
			}
		}
		// Faction 1 con Faction 2 (blocca passaggio, effetti attivi ad area, ecc)
		
		// Si aggiungeranno Effect1 con faction2, Effect 2 con faction 1 per gli effetti ad area/zona
		
		
	}
	
	public boolean collisionExists(Entity e1, Entity e2) {
		Position p1 = positionMapper.get(e1);
		Position p2 = positionMapper.get(e2);
		if (p1 != null && p2 != null) { // controlli formali ma inutili per ora, tutti gli appartenenti ai gruppi avranno Position
			Renderable r1 = renderableMapper.get(e1);
			Renderable r2 = renderableMapper.get(e2);
			float size1 = 0;
			float size2 = 0;
			if (r1 != null) {
				size1 = r1.andRenderable.halfX;
			} else {
				size1 = p1.collisionRadius;
			}
			if (r2 != null) {
				size2 = r2.andRenderable.halfX;
			} else {
				size2 = p2.collisionRadius;
			}
			size1 += size2;
			return Utils.distance(p1.x, p1.y, p2.x, p2.y) < size1;
		}
		return false;
	}

}
